import PureMachineSearchList from '../components/machine-search-list';
import * as MachineAction from 'actions/machine';
import { getFetchStatus } from 'actions/util';
import * as React from 'react';
import * as ReactRedux from 'react-redux';

import MachineSourceManage from './machine-source-manage-company';
import NewMachine from './new-machine-company';
import MachineLog from './machine-log';

const mapStateToProps = (state) => {
  return {
    isFetching: getFetchStatus(state, MachineAction.searchMachineByCompanyId),
    machineSourceFetching: getFetchStatus(state, MachineAction.listMachineSourcesByCompanyId),
    machineSources: state.companymMachineSourceList,
    machines: state.machineList.list,
    total: state.machineList.total,
    MachineSourceManage,
    NewMachine,
    MachineLog
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
    onFormSubmit(query, companyId) {
      dispatch(MachineAction.searchMachineByCompanyId(query, companyId));
    },
    initFetch(companyId) {
      dispatch(MachineAction.cleanMachineList());
      return dispatch(MachineAction.listMachineSourcesByCompanyId(companyId));
    },
    onInitialize(machine) {
      dispatch(MachineAction.initialize(machine));
    },
    onProviding(machine) {
      dispatch(MachineAction.provide(machine));
    },
    onLogging(machine, ns) {
      dispatch(MachineAction.log(machine, ns));
    },
    onMachineAssign: (machineId, companyId) => {
      return dispatch(MachineAction.assign(machineId, companyId));
    },
    onDeleting(machine) {
      dispatch(MachineAction.remove(machine));
    }
  };
};

class MachineSearchList extends React.Component {
  componentDidMount() {
    this.props.initFetch(this.props.companyId);
  }

  render() {
    return <PureMachineSearchList {...this.props}/>;
  }
}

export default ReactRedux.connect(mapStateToProps, mapDispatchToProps)(MachineSearchList);
